package com.clover.framework.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>Description: 类型转换</p>
 * <p>@Package：com.ucap.cloud_backweb.utils </p>
 * <p>Title: Convert </p>
 * <p>Company: 开普互联 </p>
 * <p>@author：masl </p>
 * <p>@date：2016-7-22下午3:14:37 </p>
 */
public class Convert {
	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，日期转字符串
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String date2Str(Date date) {
		return date2Str(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，字符串转日期
	 * @param date
	 * @return
	 */
	public static Date str2Date(String date) {
		if (StringUtils.isNotEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return new Date();
		} else {
			return null;
		}
	}

	/**
	 * 按照参数format的格式，日期转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		} else {
			return "";
		}
	}

	/**
	 * 字符串转BigDecimal
	 * 
	 * @param input
	 * @return
	 */
	public static BigDecimal str2Decimal(String input) {
		BigDecimal result = new BigDecimal("0");
		if (StringUtils.isNotEmpty(input)) {
			try {
				return new BigDecimal(input);
			} catch (Exception e) {
			}
		}
		return result;
	}
	/**
	 * 字符串转 Float
	 * 
	 * @param input
	 * @return
	 */
	public static Float str2Float(String input) {
		Float result = new Float("0");
		if (StringUtils.isNotEmpty(input)) {
			try {
				return new Float(input);
			} catch (Exception e) {
			}
		}
		return result;
	}
	public static int obj2Int(Object s) {
		int v = 0;
		if (s != null)
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception localException) {
			}
		return v;
	}

	public static Integer obj2Integer(Object s) {
		int v = 0;
		if (s != null)
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception localException) {
			}else{
				return null;
			}
		return v;
	}

	public static float obj2Float(Object s) {
		float v = 0.0F;
		if (s != null)
			try {
				v = Float.parseFloat(s.toString());
			} catch (Exception localException) {
			}
		return v;
	}

	public static double obj2Double(Object s) {
		double v = 0.0D;
		if (s != null)
			try {
				v = Double.parseDouble(obj2Str(s));
			} catch (Exception localException) {
			}
		return v;
	}

	public static boolean obj2Boolean(Object s) {
		boolean v = false;
		if (s != null)
			try {
				v = Boolean.parseBoolean(s.toString());
			} catch (Exception localException) {
			}
		return v;
	}

	public static String obj2Str(Object s) {
		return s == null ? "" : s.toString().trim();
	}

	public static Long obj2Long(Object s) {
		Long v = Long.valueOf(-1L);
		if (s != null)
			try {
				v = Long.valueOf(Long.parseLong(s.toString()));
			} catch (Exception localException) {
			}
		return v;
	}

}
